package rlog

import (
	"archive/zip"
	"io"
	"os"
	"path"
)

type Level byte

const (
	LevelDebug = iota
	LevelInfo
	LevelSuccess
	LevelWarn
	LevelError
	LevelPanic
	LevelDisable = 100
)

type RLogger interface {
	Print(a ...any)
	Println(a ...any)
	Printf(format string, a ...any)

	Debug(a ...any)
	DebugF(format string, a ...any)
	Info(a ...any)
	InfoF(format string, a ...any)
	Success(a ...any)
	SuccessF(format string, a ...any)
	Warn(a ...any)
	WarnF(format string, a ...any)
	Error(a ...any)
	ErrorF(format string, a ...any)
	Panic(a ...any)
	PanicF(format string, a ...any)
	ExitError(a ...any)          //退出程序并且打印错误，并且立即退出关闭程序，os.Exit(1)。
	ExitErrorAndSleep3(a ...any) //退出程序并且打印错误，延时3秒后退出关闭程序，os.Exit(1)。
	Recover()                    //协程崩溃恢复，并且打印崩溃日志。
	recover(content string)

	PrintlnConsoleMust(a ...any)     //强制打印到控制台日志
	PrintlnFileMust(a ...any)        //强制打印到文件
	PrintlnConsoleMustOnly(a ...any) //强制只打印到控制台日志
	PrintlnFileMustOnly(a ...any)    //强制只打印到文件

	SetLogFilePath(filepath string, oldLogToZip bool) //设置日志文件路径
	SetConsoleLevel(level Level)                      //设置控制台日志打印等级
	SetFileLevel(level Level)                         //设置文件日志打印等级
	SetFullTextStaining(full bool)                    //设置是否全文着色，默认false：tag 着色。
	SetAutoDeleteOldLogFile(day uint)                 //设置自动删除多少天前的日志
	SetLogFileMaxSize(mb uint)                        //设置日志文件最大大小，MB

	SetRuntimeCaller(skip int)
}

// zipSaveAs 保存为zip文件
func zipSaveAs(srcFilepath string, newName string, zipFilepath string, isDeleteSrc bool) error {
	srcFile, err := os.Open(srcFilepath)
	if err != nil {
		return err
	}
	zFile, err := os.Create(zipFilepath)
	if err != nil {
		return err
	}
	defer zFile.Close()
	writer := zip.NewWriter(zFile)
	defer writer.Close()
	if newName == "" {
		newName = path.Base(srcFilepath)
	}
	create, err := writer.Create(newName)
	if err != nil {
		return err
	}
	_, err = io.Copy(create, srcFile)
	srcFile.Close()
	if isDeleteSrc {
		_ = os.Remove(srcFilepath)
	}
	return err
}
